from django.db import models

from mlh.utils.model import BaseModel
from users.models import User


class Tag(models.Model):
    """
    问题标签
    """
    tag_name = models.CharField(max_length=32, verbose_name='标签')

    class Meta:
        db_table = 'qa_tag'
        verbose_name = '标签'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.tag_name


class Question(BaseModel):
    """
    提问
    """
    title = models.CharField(max_length=64, verbose_name='标题')
    tag = models.CharField(max_length=16, verbose_name='标签')
    q_content = models.TextField(verbose_name='正文')
    page_view = models.IntegerField(default=0, verbose_name='浏览量')
    use_num = models.IntegerField(default=0, verbose_name='有用')
    ans_num = models.IntegerField(default=0, verbose_name='回答')
    # user_id = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name='作者')

    class Meta:
        db_table = 'qa_question'
        verbose_name = '问题'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class Answer(BaseModel):
    """
    回答
    """
    user_id = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name='楼主')
    question_id = models.ForeignKey(Question, on_delete=models.PROTECT, verbose_name='问题')
    a_content = models.CharField(max_length=1024, verbose_name='答案')

    class Meta:
        db_table = 'qa_answer'
        verbose_name = '答案'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.a_content


class QuestionFlag(models.Model):
    """
    问题的点赞和点踩
    """
    question_id = models.ForeignKey(Question, on_delete=models.PROTECT, verbose_name='问题')
    user_id = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name='赞踩者')
    q_flag_praise = models.NullBooleanField(verbose_name='是否赞')
    q_flag_tread = models.NullBooleanField(verbose_name='是否踩')

    class Meta:
        db_table = 'qa_question_flag'
        verbose_name = '问题评价'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.question_id


class AnswerFlag(models.Model):
    """
    答案的点赞和点踩
    """

    answer_id = models.ForeignKey(Answer, on_delete=models.PROTECT, verbose_name='答案')
    user_id = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name='赞踩者')
    a_flag_praise = models.NullBooleanField(verbose_name='是否赞')
    a_flag_tread = models.NullBooleanField(verbose_name='是否踩')

    class Meta:
        db_table = 'qa_answer_flag'
        verbose_name = '答案评价'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.answer_id
